import { defineStore } from "pinia";
import type {reqCategoryReturnType} from '@/api/attr'
import {requestCategoryList1,requestCategoryList2,requestCategoryList3} from '@/api/attr'
import { ElMessage } from "element-plus";

//state参数类型
interface stateType{
    categoryList1: reqCategoryReturnType,
    categoryList2: reqCategoryReturnType,
    categoryList3: reqCategoryReturnType,
    category1Id: number | null,
    category2Id: number | null,
    category3Id: number | null
}
const useAttrStore = defineStore('attr',{
    state:():stateType => {
        return {
            categoryList1: [],
            categoryList2: [],
            categoryList3: [],
            category1Id: null,
            category2Id: null,
            category3Id: null
        }
    },
    getters:{},
    actions:{
        //获取一级分类列表
        async getCategory1(){
            try {
                //发送请求
                const re = await requestCategoryList1()
                //把一级分类列表赋值给state
                this.categoryList1 = re
            } catch (e) {
                ElMessage.error('获取一级分类列表失败')
            }
        },
        //获取二级分类列表
        async getCategory2(){
            try {
                //发送请求
                const re = await requestCategoryList2(this.category1Id as  number)
                //把二级分类列表赋值给state
                this.categoryList2 = re
            } catch (e) {
                ElMessage.error('获取二级分类列表失败')
            }
        },
        //获取三级分类列表
        async getCategory3(){
            try {
                //发送请求
                const re = await requestCategoryList3(this.category2Id as number)
                //把三级分类列表赋值给state
                this.categoryList3 = re
            } catch (e) {
                ElMessage.error('获取三级分类列表失败')
            }
        },
    }
})

export default useAttrStore